package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.AppUserGainDao;
import com.leoman.dao.VideoRecordDao;
import com.leoman.dao.VideoResumeDao;
import com.leoman.entity.VideoRecord;
import com.leoman.entity.VideoResume;
import com.leoman.service.VideoRecordService;
import com.leoman.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * VideoRecordServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:26.
 */
@Service
public class VideoRecordServiceImpl implements VideoRecordService {

    @Autowired
    private VideoRecordDao videoRecordDao;

    @Autowired
    private AppUserGainDao appUserGainDao;

    @Autowired
    private VideoResumeDao videoResumeDao;

    @Override
    public List<VideoRecord> findAll() {
        return videoRecordDao.findAll();
    }

    @Override
    public Page<VideoRecord> find(int pageNum, int pageSize) {
        return videoRecordDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<VideoRecord> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public VideoRecord getById(int id) {
        return videoRecordDao.findOne(id);
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        // 删除所有跟该视频相关联的简历投放记录
        List<VideoResume> list = videoResumeDao.iFindListByVideoRecordId(id);

        for (VideoResume videoResume : list) {
            videoResumeDao.delete(videoResume);
        }

        videoRecordDao.delete(id);
    }

    @Override
    public VideoRecord create(VideoRecord videoRecord) {
        return videoRecordDao.save(videoRecord);
    }

    @Override
    public VideoRecord update(VideoRecord videoRecord) {
        return videoRecordDao.save(videoRecord);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<VideoRecord> findListByVideoId(Integer videoId) {
        List<VideoRecord> list = videoRecordDao.findListByVideoId(videoId);
        Long nowTime;
        // 转换数据
        for (VideoRecord videoRecord : list) {
            videoRecord.setCreateTimeStr(null != videoRecord.getCreateTime() ? DateUtils.longToStringWithFormat(videoRecord.getCreateTime(), "yyyy-MM-dd hh:mm:ss") : "");
            videoRecord.setStartTimeStr(null != videoRecord.getStartTime() ? DateUtils.longToStringWithFormat(videoRecord.getStartTime(), "yyyy-MM-dd hh:mm:ss") : "");
            videoRecord.setEndTimeStr(null != videoRecord.getEndTime() ? DateUtils.longToStringWithFormat(videoRecord.getEndTime(), "yyyy-MM-dd hh:mm:ss") : "");
            if (!videoRecord.getSendType().equals(Constant.VIDEO_SEND_TYPE_FREE)) {
                if (null != videoRecord.getEndTime()) {
                    videoRecord.setRecordStatus(Constant.VIDEO_SHOW_STATUS_SIX);
                } else {
                    videoRecord.setRecordStatus(Constant.VIDEO_SHOW_STATUS_FOUR);
                    videoRecord.setRecordContent("  剩余￥" + VideoServiceImpl.getLaveMoney(videoRecord, appUserGainDao));
                }
            } else {
                videoRecord.setRecordStatus(Constant.VIDEO_SHOW_STATUS_SIX);
                nowTime = System.currentTimeMillis();
                if (videoRecord.getEndTime() > nowTime && videoRecord.getStartTime() <= nowTime) {
                    videoRecord.setRecordStatus(Constant.VIDEO_SHOW_STATUS_FOUR);
                }
            }
        }

        return list;
    }
}